package com.app.shortvideo

import android.annotation.SuppressLint
import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bytedance.sdk.openadsdk.api.nativeAd.PAGNativeAd
import com.bytedance.sdk.openadsdk.api.nativeAd.PAGNativeAdInteractionListener

class VideoAdView : RelativeLayout {
    constructor(context: Context?,feedAds: MutableList<PAGNativeAd>) : super(context)
    {
        createFeedAdView(feedAds)
    }
    constructor(context: Context?, attrs: AttributeSet?,feedAds: MutableList<PAGNativeAd>) : super(context, attrs)
    {
        createFeedAdView(feedAds)
    }
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int,feedAds: MutableList<PAGNativeAd>) : super(
        context,
        attrs,
        defStyleAttr
    ){
        createFeedAdView(feedAds)
    }

    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
        feedAds: MutableList<PAGNativeAd>
    ) : super(context, attrs, defStyleAttr, defStyleRes)
    {
        createFeedAdView(feedAds)
    }
    private fun createFeedAdView(feedAds: MutableList<PAGNativeAd>): View? {
        if (feedAds.isEmpty()) {
            return null
        }
        val nativeAd: PAGNativeAd = feedAds.removeAt(0)
        val nativeAdData = nativeAd.nativeAdData

        val adRootView = LayoutInflater.from(this.context).inflate(R.layout.feed_ad_view, null)
        (adRootView.findViewById<View>(R.id.tt_ad_logo) as RelativeLayout).addView(nativeAdData.adLogoView)
        (adRootView.findViewById<View>(R.id.tv_listitem_ad_title) as TextView).text =
            nativeAdData.title
        (adRootView.findViewById<View>(R.id.tv_listitem_ad_desc) as TextView).text =
            nativeAdData.description
        val icon = nativeAdData.icon
        if (icon != null && icon.imageUrl != null) {
            Glide.with(this).load(icon.imageUrl)
                .into(((adRootView.findViewById<View>(R.id.iv_listitem_icon) as ImageView)))
        }
        (adRootView.findViewById<View>(R.id.tt_creative_btn) as TextView).text =
            if (TextUtils.isEmpty(nativeAdData.buttonText)) "View" else nativeAdData.buttonText

        val mediaView = nativeAdData.mediaView
        if (mediaView != null) {
            (adRootView.findViewById<View>(R.id.iv_listitem_video) as FrameLayout).addView(mediaView)
        }

        val clickViewList = ArrayList<View>()
        clickViewList.add(adRootView)
        val clickCreativeList = ArrayList<View>()
        clickCreativeList.add(adRootView.findViewById(R.id.tt_creative_btn))
        nativeAd.registerViewForInteraction(
            ((adRootView as ViewGroup)),
            clickViewList,
            clickCreativeList,
            null,
            object : PAGNativeAdInteractionListener{
                override fun onAdShowed() {
                }

                override fun onAdClicked() {
                }

                override fun onAdDismissed() {
                }
            })

        return adRootView
    }
}